#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() {
    printf("testexec begin ...\n");
    
    pid_t id = fork();
    if (id == 0) {
        // 程序替换，多进程执行
        sleep(2);
        char* const argv[] = {
           (char*) "test"
            ,(char*)"-a"
            ,(char*)"-l"
            ,(char*)"-n"
            ,NULL
        };
        char* const envp[] = {
            (char*)"HHHHHH=1111111",
            (char*)"EEEEEE=2233333",
            (char*)"DDDD=222222",
            NULL
        };
        execvpe("./test", argv, envp); 
        //execvp("ls", argv);
        //execlp("top", "top", NULL);
        //execv("/usr/bin/ls", argv);
        //execl("/usr/bin/ls", "ls", "-a", "-l", NULL);
        exit(1); // replace fail, set exit code : 1
    }
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0) 
        printf("wait succss! exit code: %d\n", WEXITSTATUS(status));
    else
        printf("wait fail!");
    printf("testexec over ...\n");
    return 0;
}
